require "scripts.log"

local mqtt = newMQTT()

function line_handle(key, payload)
    local topic = string.format("logs/line/%s", key)
    if mqtt:publish(topic, payload, 0) < 0 then
        loge("publish failed. [%s] %s", topic, payload)
    end
end

function mqtt_on_message(topic, payload) 
    --logi("[mqtt] << [%s] %s", topic, payload)
    idx = string.match(topic, ".*/(.+)$")

    line_handle(tonumber(idx), payload)
end

function mqtt_on_connected(remote_address, remote_port)
    logi("Connected to MQTT broker. %s <-> %s", remote_address, remote_port);
end

if mqtt:open("ll-" .. os.time(), "zx", "123456", 180, "127.0.0.1", 21883, 
    "mqtt_on_connected", "mqtt_on_message") == 0 then

    logpath="C:/Users/zz/Desktop/1.log"

    local keywords1 = {}
    keywords1["1"] = " >>>>> ip:"
    keywords1["2"] = " <<<<< remote"
    fileWatcher1 = newFileWatcher(logpath, keywords1, 0, "line_handle")

    logpath2="C:/Users/zz/Desktop/2.log"

    local keywords2 = {}
    keywords2["BD1"] = "[DL>]"
    keywords2["BD2"] = "[DL<]"
    fileWatcher2 = newFileWatcher(logpath2, keywords2, 0, "line_handle")
end